package Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import Dominion.*;

public class AuthorDAO extends CrudDAO<Author>{

    @Override
    public Author create(Author autor) {
        String sqlQuerry = "INSERT INTO Author (email,karma) VALUES ('"
        		+autor.getEmail()+"','"+autor.getKarma()+"')";
        try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return null;
    }

    @Override
    public void delete(Author autor) {
    	 try {
 			AgenteBD.getDbCon().queryUpdate("DELETE FROM Author WHERE email='"+ autor.getEmail() +"'");
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
    }

    @Override
    public Author read(Author autor) throws SQLException {
      ResultSet rs = AgenteBD.getDbCon().query(
                "SELECT * FROM Author WHERE email=" + autor.getEmail()+ "");
        if (rs != null) {
            try {
                while (rs.next()) {
                    autor.setEmail(rs.getString("email"));
                    autor.setKarma(rs.getInt("karma"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        rs.close();
        return autor;
    }

    @Override
    public void update(Author autor) {
    	 String sqlQuerry="UPDATE Author SET email='"+autor.getEmail()
		         +"', karma='" + autor.getKarma()
		         + "' WHERE email='" + autor.getEmail()+ "'";
		try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}       
    }
}
